package com.webwork.lims.transport.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.lims.common.dao.impl.SimpleDao;
import com.webwork.lims.transport.dao.WaysDao;
import com.webwork.lims.transport.dataobject.TransType;
import com.webwork.lims.transport.dataobject.Ways;
import com.webwork.lims.util.ColumnLimit;
import com.webwork.lims.util.DBSimpleSQL;

@Repository
public class WaysDaoImpl extends SimpleDao implements WaysDao {

	public WaysDaoImpl() {
		super();
		this.dbTable = "t_ways";
		dbColumns.clear();
		dbColumns.put("wayCode", "WAY_CODE");
		dbColumns.put("startCode", "START_CODE");
		dbColumns.put("endCode", "END_CODE");
		dbColumns.put("tranType", "TRAN_TYPE");
		dbColumns.put("hour", "HOUR");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof Ways))
			return new ColumnLimit();
		Ways o = (Ways) object;
		ColumnLimit ret = new ColumnLimit();
		if (o.getWayCode() != 0) {
			ret.add(dbColumns.get("wayCode") + " = ");
		}
		if (o.getStartCode() != 0) {
			ret.add(dbColumns.get("startCode") + " = ");
		}
		if (o.getEndCode() != 0) {
			ret.add(dbColumns.get("endCode") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		Ways o = (Ways) object;
		List<Object> ret = new ArrayList<Object>();
		if (o.getWayCode() != 0) {
			ret.add(o.getWayCode());
		}
		if (o.getStartCode() != 0) {
			ret.add(o.getStartCode());
		}
		if (o.getEndCode() != 0) {
			ret.add(o.getEndCode());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		Ways o = (Ways) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getWayCode());
		objs.add(o.getStartCode());
		objs.add(o.getEndCode());
		objs.add(o.getType().name());
		objs.add(o.getHour());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		Ways ret = new Ways();
		ret.setWayCode((Integer) map.get(dbColumns.get("wayCode")));
		ret.setStartCode((Integer) map.get(dbColumns.get("startCode")));
		ret.setEndCode((Integer) map.get(dbColumns.get("endCode")));
		ret.setType(TransType.valueOf((String) map.get(dbColumns.get("tranType"))));
		ret.setHour((Integer) map.get(dbColumns.get("hour")));
		return ret;
	}
	
	@Override
	public void insert(Ways o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(Ways o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("wayCode") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getWayCode());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public void delete(int id) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("wayCode") + " = ");
		String sql = DBSimpleSQL.del(dbTable, limit);
		this.getJdbcTemplate().update(sql, id);
	}

	@Override
	public List<Ways> query(Ways o, int page, int size) {
		List<Ways> ret = new ArrayList<Ways>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((Ways) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(Ways o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
